<?php

namespace App\Model\Backend;

use Illuminate\Database\Eloquent\Model;
use Psy\Test\CodeCleaner\AbstractClassPassTest;


class ItemModel extends Model
{
    protected $table = 'items';
    protected $primaryKey = 'id';

//项目回复
    public function posts(){
    return $this->hasMany('App\Model\backend\ItemPostsModel','items_id','id');
    }
    //项目基本信息
    public function ShowInfo($id){

        $infos=$this->find($id);
        $infos['posts']=$this->find($id)->posts()->orderBy('created_at','DESC')->get();
        return $infos;
    }

    public function tasks(){
        return $this->hasMany('App\Model\backend\ItemTasksModel','item_id','id');
    }
    //项目任务展示
    public function tasksShow($id){
        $member=new Member();
        $task=new ItemTasksModel();
        $itemInfo=$this->find($id);

        $itemTasks=$itemInfo->tasks()->orderby('task_level','ASC')->orderby('task_id','ASC')->get();
        $stages=new ProjectStagesModel();
        $stagesarr=array();
        foreach($itemTasks as $k=>$v){
            if($v['fid']>0||$v['task_status']=="-10"){
//                unset($itemTasks[$k]);
//                continue;
            }
            $name1=$member->find($v['member_id_task']);
            $name2=$member->find($v['member_id_check']);
            $itemTasks[$k]['name1']=$name1['username'];
            $itemTasks[$k]['name2']=$name2['username'];
           $stagesarr[$v['stage_id']]=$stages->find($v['stage_id'])['name'];
            $childs=$task->where(['fid'=>$v['id']])->get();
            foreach($childs as $i=>$y){
                if($y['task_status']=="-10"){
                    continue;
                }
                $child_name=$member->find($y['member_id_task']);
                $y['child_name1']=$child_name['username'];
                $y['child_name2']=$name1['username'];
                $a[$i]=$y;
                $itemTasks[$k]['childs'] =$a;
            }
        }
        $itemTasks['stages']=$stagesarr;
        return $itemTasks;
    }

    public function userInfo($id){
        $arr=array();
        $template=new TemplatesStagesTaskModel();
        $item=new ItemTasksModel();
        $a=$item->where(['item_id'=>$id])->first();

        $taskInfo=$template->where(['tid'=>$a['template_id']])->orderBy('level','ASC')->get();
        $member=new Member();
        $role=new RoleModel();
        $roles="";
        foreach($taskInfo as $k=>$v){
           $roles .=$v['type_id'].",".$v['role_id'].",";
        }
        $rolesarry=array_unique(explode(',',trim($roles,',')));
       // $r=implode(',',$rolesarry);
        $users=$role->whereIn('rid',$rolesarry)->get();
        foreach($users as $k=>$v){
           $res= $member->find($v['uid']);
            $arr[$v['rid']][$k]['uid']=$res['uid'];
            $arr[$v['rid']][$k]['username']=$res['username'];
        }

        return $arr;
    }



}
